package com.gzhryc.common.event;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 定时执行延迟事件
 * @author zhanghao
 * @version 2020-12-21
 */
public abstract class TimerDelayEvent implements Delayed {
	
	private Long startTimes;
	
	public TimerDelayEvent() {
		this.startTimes = System.currentTimeMillis();
	}

	@Override
	public int compareTo(Delayed o) {
		long result = this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS);
		if (result < 0) {
			return -1;
		} else if (result > 0) {
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public long getDelay(TimeUnit unit) {
		return unit.convert(startTimes  + this.getExecuteTimes() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
	}

	/**
	 * 获取延迟执行时长，单位：毫秒
	 * @return
	 */
	public abstract Long getExecuteTimes();

}
